package fun.yyds.common.page;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 分页工具类
 *
 * @author lxb
 */
@Data
@NoArgsConstructor
public class PageData<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    /**
     * 总记录数
     */
    @ApiModelProperty("总数")
    private int totalCount;
    /**
     * 每页记录数
     */
    @ApiModelProperty("分页大小")
    private int pageSize;
    /**
     * 总页数
     */
    @ApiModelProperty("总页数")
    private int totalPage;
    /**
     * 当前页数
     */
    @ApiModelProperty("当前页数")
    private int currPage;
    /**
     * 列表数据
     */
    @ApiModelProperty("数据列表")
    private List<T> list = new ArrayList<>();

    // 用于下一次查询的searchAfter值
    private Object[] searchAfter;

    /**
     * 分页
     *
     * @param list     列表数据
     * @param total    总记录数
     * @param pageSize 每页记录数
     * @param currPage 当前页数
     */
    public PageData(List<T> list, int total, int pageSize, int currPage) {
        this.list = list;
        this.totalCount = total;
        this.pageSize = pageSize;
        this.currPage = currPage;
        this.totalPage = (int) Math.ceil((double) total / pageSize);
    }

    /**
     * 分页
     *
     * @param list     列表数据
     * @param total    总记录数
     * @param pageSize 每页记录数
     * @param currPage 当前页数
     */
    public PageData(List<T> list, int total, int pageSize, int currPage, Object[] searchAfter) {
        this.list = list;
        this.totalCount = total;
        this.pageSize = pageSize;
        this.currPage = currPage;
        this.totalPage = (int) Math.ceil((double) total / pageSize);
        this.searchAfter = searchAfter;
    }

    public static <T> PageData<T> getInstance(IPage<?> page, Class<T> t) {
        PageData<T> result = new PageData<>();
        if (ObjectUtil.isNotEmpty(page.getRecords())) {
            result.list = BeanUtil.copyToList(page.getRecords(), t);
        } else {
            result.list = new ArrayList<>();
        }
        result.totalCount = (int) page.getTotal();
        result.pageSize = (int) page.getSize();
        result.currPage = (int) page.getCurrent();
        result.totalPage = (int) page.getPages();
        return result;
    }

    public static <T> PageData<T> getInstance(IPage<T> page) {
        PageData<T> result = new PageData<>();
        result.list = page.getRecords();
        result.totalCount = (int) page.getTotal();
        result.pageSize = (int) page.getSize();
        result.currPage = (int) page.getCurrent();
        result.totalPage = (int) page.getPages();
        return result;
    }

    public static <T> PageData<T> getInstance(int page, int limit) {
        PageData<T> result = new PageData<>();
        result.list = new ArrayList<>();
        result.totalCount = 0;
        result.pageSize = page;
        result.currPage = limit;
        result.totalPage = 0;
        return result;
    }
}
